<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属门店" prop="mid">
          <el-select v-model="queryParams.mid" placeholder="请选择门店" clearable>
            <el-option
              v-for="item in mds"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hatha:ykecheng:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hatha:ykecheng:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hatha:ykecheng:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hatha:ykecheng:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ykechengList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <!-- <el-table-column label="所属日期" align="center" prop="bid" /> -->
      <el-table-column label="课程" align="center" prop="kcname" />
      <el-table-column label="教室" align="center" prop="rname" />
      <el-table-column label="教师" align="center" prop="tname" />
      <el-table-column label="上课时间" align="center" prop="timeStr" />
      <!-- <el-table-column label="时长" align="center" prop="shichang" /> -->
      <el-table-column label="门店" align="center" prop="mname" />
      <el-table-column label="已预约" align="center" prop="yyy" />
      <el-table-column label="管理" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sunny"
            @click="viewinfo(scope.row.id)"
            v-hasPermi="['hatha:ykecheng:edit']"
          >查看预约信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="quxiao(scope.row)"
            v-hasPermi="['hatha:ykecheng:edit']"
          >取消所有预约</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="quxiao(scope.row)"
            v-hasPermi="['hatha:ykecheng:edit']"
          >取消所有预约</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hatha:ykecheng:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hatha:ykecheng:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属门店" prop="mid">
          <el-select @change="mdchange" v-model="form.mid" placeholder="请选择门店" clearable>
            <el-option
              v-for="item in mds"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="选择课程" prop="kid">
          <el-select v-model="form.kid" placeholder="请选择课程" clearable>
            <el-option
              v-for="item in kcs"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="选择老师" prop="teacher">
          <el-select v-model="form.teacher" placeholder="请选择老师" clearable>
            <el-option
              v-for="item in lss"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="选择教室" prop="roomId">
          <el-select v-model="form.roomId" placeholder="请选择老师" clearable>
            <el-option
              v-for="item in jss"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="上课时间" prop="timeStr">
          <el-input v-model="form.timeStr" placeholder="请输入上课时间" />
        </el-form-item>
        <el-form-item label="时长" prop="shichang">
          <el-input v-model="form.shichang" placeholder="请输入时长" />
        </el-form-item>
        <el-form-item label="最大人数" prop="maxnum">
          <el-input v-model="form.maxnum" placeholder="请输入最大人数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog v-if="yyInfo!=null" title="预约信息" :visible.sync="showyyinfo" :before-close="handleClose">
      <el-table :data="yyInfo">
        <el-table-column property="user.nickName" label="名称" width="150"></el-table-column>
        <el-table-column property="user.phonenumber" label="手机" width="200"></el-table-column>
        <el-table-column property="createtime" label="预约时间"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listYkecheng, getYkecheng, delYkecheng, addYkecheng, updateYkecheng,quxiaoYkecheng,yyinfo } from "@/api/hatha/ykecheng";
import { listKecheng } from "@/api/hatha/kecheng";
import { listTeachers } from "@/api/hatha/teachers";
import { listRooms } from "@/api/hatha/rooms";
import { listMendian } from "@/api/hatha/mendian";
export default {
  name: "Ykecheng",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程列表格数据
      ykechengList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bid: null,
        kid: null,
        roomId: null,
        teacher: null,
        timeStr: null,
        shichang: null,
        mid:null,
        maxnum:null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
      },
      lss:[],
      jss:[],
      kcs:[],
      mds:[],
      showyyinfo:false,
      yyInfo:null

    };
  },
  created() {

    const id = this.$route.params && this.$route.params.id;
    if (id) {
      this.loading = true;

      this.queryParams.bid = id
      this.form.bid = id
      listYkecheng(this.queryParams).then(response => {
        this.ykechengList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    }
    this.getmendian()
    //this.getList();
  },
  methods: {
    handleClose(){
      this.yyInfo = null
      this.showyyinfo = false
    },
    viewinfo(id){
      yyinfo(id).then(res => {
        this.yyInfo = res.data
        this.showyyinfo = true
      })
    },
    mdchange(e){
      console.log(e)
      this.getkecheng(e)
      this.getTeacher(e)
      this.getRooms(e)
    },
    getkecheng(e){
      listKecheng({mid:e}).then(res => {
        this.kcs = res.rows
      })
    },
    getmendian(){
      listMendian().then(res => {
        this.mds = res.rows
      })
    },
    getTeacher(e){
      listTeachers({mid:e}).then(res => {
        this.lss = res.rows
      })
    },
    getRooms(e){
      listRooms({mid:e}).then(res => {
        this.jss = res.rows
      })
    },
    /** 查询课程列列表 */
    getList() {
      this.loading = true;
      listYkecheng(this.queryParams).then(response => {
        this.ykechengList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bid: null,
        kid: null,
        roomId: null,
        teacher: null,
        timeStr: null,
        shichang: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getYkecheng(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程列";
      });
    },
    /** 提交按钮 */
    submitForm() {

      const id = this.$route.params && this.$route.params.id;
      this.form.bid = id


      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateYkecheng(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addYkecheng(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程列编号为"' + ids + '"的数据项？').then(function() {
        return delYkecheng(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },


    quxiao(row){
      const id = row.id
      this.$modal.confirm('是否确认取消本课程的所有预约？').then(function() {
        return quxiaoYkecheng(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('hatha/ykecheng/export', {
        ...this.queryParams
      }, `ykecheng_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
